<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div></div>
    <script>
      // 获取div元素
      var div = document.querySelector("div");

      function time() {
        var myDate = new Date(); // 创建时间日期对象
        var y = myDate.getFullYear();
        var m = (myDate.getMonth() + 1).toString().padStart(2, "0");
        var d = myDate.getDate().toString().padStart(2, "0");
        var h = myDate.getHours().toString().padStart(2, "0");
        var f = myDate.getMinutes().toString().padStart(2, "0");
        var s = myDate.getSeconds().toString().padStart(2, "0");
        var str = `${y}-${m}-${d} ${h}:${f}:${s}`;
        div.innerHTML = str;
      }

      time();
      // 定时器
      setInterval(time, 1000);
    </script>
  </body>
</html>
